import 'package:flutter/material.dart';

class EgTextField extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
//    String _inputText = "";
//    final TextEditingController controller = new TextEditingController();
//    controller.addListener(() {
//      print("您输入的内容为 ${controller.text}");
//    });

//    final TextEditingController controller2 = new TextEditingController.fromValue(TextEditingValue(
//        text: _inputText, selection: TextSelection.fromPosition(TextPosition(affinity: TextAffinity.downstream, offset: _inputText.length))));
//    controller2.addListener(() {
//      _inputText = controller2.text;
//      print("您输入的内容为 ${controller2.text}");
//    });

    return new Scaffold(
      key: Key("scaffold"),
      appBar: new AppBar(
        title: new Text("演示文本框组件"),
      ),
      body: new Center(
        child: Padding(padding: EdgeInsets.all(20.0), child: new StateTextField()),
      ),
    );
  }
}

class StateTextField extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new _StateTextField();
  }
}

class _StateTextField extends State<StatefulWidget> {
  String _inputText = "";

  @override
  Widget build(BuildContext context) {
    TextEditingController controller = new TextEditingController.fromValue(TextEditingValue(
        text: _inputText, selection: TextSelection.fromPosition(TextPosition(affinity: TextAffinity.downstream, offset: _inputText.length))));
    controller.addListener(() {
      _inputText = controller.text;
      print("您输入的内容为 ${controller.text}");
    });

    return new TextField(
      controller: controller,
      maxLength: 10,
      maxLines: 1,
      autocorrect: true,
      autofocus: true,
      obscureText: false,
      textAlign: TextAlign.left,
      style: new TextStyle(fontSize: 26, color: Colors.blue),
      onChanged: (text) {
        this.setState(() {
          _inputText = controller.text;
        });
        print("内容发生了变化 $text");
      },
      onSubmitted: (text) {
        print("内容提交时的值 $text");
      },
      enabled: true,
      decoration:
          InputDecoration(fillColor: Colors.grey.shade200, filled: true, helperText: "用户名", prefixIcon: Icon(Icons.person), suffixText: "用户名"),
    );
  }
}
